<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card class="box-card">
      <!-- 添加角色按钮 -->
      <el-row>
        <el-button type="primary" @click="addShow">添加分类</el-button>
      </el-row>

      <!-- 分类表格 -->

      <zk-table
        ref="table"
        sum-text="sum"
        index-text="#"
        :data="catelist"
        :columns="columns"
        :selection-type="false"
        :show-index="true"
        :expand-type="false"
        border
      >
        <!-- 是否有效 -->
        <template slot="isok" slot-scope="scope">
          <i v-if="scope.row.cat_deleted===false" style="color:green" class="el-icon-success"></i>
          <i v-else class="el-icon-error" style="color:green"></i>
        </template>
        <!-- 排序 -->
        <template slot="order" slot-scope="scope">
          <el-tag size="mini">一级</el-tag>
          <el-tag type="success" size="mini">二级</el-tag>
          <el-tag type="warning" size="mini">三级</el-tag>
        </template>

        <template slot="opt" slot-scope="scope">
          <el-button size="mini" type="primary" class="el-icon-edit">编辑</el-button>
          <el-button size="mini" type="danger" class="el-icon-delete">删除</el-button>
        </template>
      </zk-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <!-- 添加分类对话框 -->
    <el-dialog title="添加分类" :visible.sync="dialogVisible" width="30%">
      <el-form
        :model="addCateForm"
        :rules="addCateRules"
        ref="addCateRef"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="活动名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <!-- props 用来指定配置对象 -->
        <!-- options 用来指定数据源 -->
        <el-form-item label="父级分类">
          <el-cascader
            :props="cascaderProps"
            :options="parentCatelist"
            expandTrigger="hover"
            v-model="selectedOptions"
            @change="prantCateChange"
            clearable
            change-on-select
          ></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 10
      },
      // 商品分类数据列表
      catelist: [],
      total: 0,
      columns: [
        {
          label: "分类名称",
          prop: "cat_name"
        },
        {
          label: "是否有效",
          //表示当前列定义为模板名称
          type: "template",
          // 表示当前这一列使用模板名称
          template: "isok"
        },
        {
          label: "排序",
          //表示当前列定义为模板名称
          type: "template",
          // 表示当前这一列使用模板名称
          template: "order"
        },
        {
          label: "排序",
          //表示当前列定义为模板名称
          type: "template",
          // 表示当前这一列使用模板名称
          template: "opt"
        }
      ],
      // 控制添加分类对话框显示隐藏
      dialogVisible: false,
      // 添加对话框验证规则
      addCateRules: {
        cat_name: [
          {
            required: true,
            message: "请输入分类名称"
          }
        ]
      },
      addCateRef: [],
      // 添加分类的表单数据
      addCateForm: {
        cat_name: "",
        // 父级分类id
        cat_pid: 0,
        // 分类的等级 默认要添加的是1级分类
        cat_level: 0
      },

      parentCatelist: [],
      // 指定级联选择器的配置对象
      cascaderProps: {
        value: "cat_id",
        label: "cat_name",
        children: "children"
      },
      // 选中的辅机分类的id数组
      selectedOptions: []
    };
  },
  created() {
    this.getCateList();
  },
  methods: {
    async getCateList() {
      const { data: res } = await this.$http.get("categories", {
        params: this.queryInfo
      });
      if (res.meta.status !== 200) return this.$message.error("获取失败！");

      console.log(res.data);
      this.catelist = res.data.result;
      this.total = res.data.total;
      console.log(this.total);
    },
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize;
      this.getCateList();
    },
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage;
      this.getCateList();
    },
    addShow() {
      // 获取父类分类的数据列表
      this.getParentCateList();
      this.dialogVisible = true;
    },
    // 获取父级分类列表
    async getParentCateList() {
      const { data: res } = await this.$http.get("categories", {
        params: { type: 2 }
      });
      if (res.meta.status !== 200) return this.$message.error("获取失败");
      this.parentCatelist = res.data;
      console.log(this.parentCatelist);
    },
    //选择项发生变化触发
    prantCateChange() {
      console.log(this.selectedOptions);
      // 如果selectedOptions length大于0 证明选中了 父级分类
      // 反之 则没有选中父级分类
      if (this.selectedOptions.length > 0) {
        // 父级分类的id
        this.addCateForm.cat_pid = this.selectedOptions[
          this.selectedOptions.length - 1
        ];
        this.addCateForm.cat_level = this.selectedOptions.length;
        return;
      } else {
        // 父级分类的id
        this.addCateForm.cat_pid = 0;
        this.addCateForm.cat_level = 0;
        return;
      }
    },
    closeDialog() {
      //监听对话框的关闭事件
      console.log(this.$refs);
      this.$refs.addCateRef.resetFields();
      this.selectedOptions = [];
      this.addCateForm.cat_level = 0;
      this.addCateForm.cat_name = 0;
    },
    // 添加分类
    addCate() {
      this.$refs.addCateRef.validate(async valid => {
        if (!valid) return;
        const { data: res } = await this.$http.post(
          "categories",
          this.addCateForm
        );
        if (res.meta.status !== 201) return this.$message.error("添加分类失败");
        this.$message.success("添加成功！");
        this.getCateList();
      });
    }
  }
};
</script>

<style lang="less" scoped>
.zk-table {
  margin-top: 20px;
}
</style>